#region File Header

//----------------------------------------------------------------
// Copyright (C) 2002 Stefan Kyntchev
// All rights reserved.
//
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY 
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT 
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR 
// FITNESS FOR A PARTICULAR PURPOSE.
//----------------------------------------------------------------

#endregion File Header

using System;
using System.Collections;
using System.IO;
using System.Runtime.InteropServices;
using System.Web;
using System.Web.UI.WebControls;
using AirWeb.ApplicationTools;
using AirWeb.ApplicationTools.Image;
//using AirWeb.IndexService;

namespace FlashBack.Web
{
	/// <summary>
	///    Summary description for SlideShow.
	/// </summary>
	public partial class SlideShow : PageBase
	{
		private const string ORG_COOKIE_NAME = "UseOrg";
		protected string m_DataSource;
		private DirectoryHelper m_dirHelper;
		public String m_Slide_Timeout = "5000";
		private ArrayList m_SlideArray;
		protected string m_StripSource;
		private String m_vDirectoryPath;
		protected string tooltipText = "";
		protected string slide;

		public SlideShow()
		{
			m_dirHelper = new DirectoryHelper();
			Page.Init += new EventHandler(Page_Init);

			//Subscribe to events from directory helper
			//The event handler class is implemented in PageBase
			m_dirHelper.StatusEvent += new StatusEventHandler(DirHelperNotifier);
		}

		protected void Page_Load(object sender, EventArgs e)
		{
			string request = Request.QueryString["path"];
			string query = Request.QueryString["qu"];

			if (!IsPostBack)
			{
				HttpCookie c = Request.Cookies[ORG_COOKIE_NAME];
				if (c != null && c.Value == "On")
					UseOriginals.Checked = true;
				else
					UseOriginals.Checked = false;
			}

			/*if (query != null)
			{
				IXQuery q = new IXQuery(""); // use the catalog from the configuration file
				try
				{
					q.Query(query);
				}
				catch (Exception exc)
				{
					COMException c = (COMException) (exc);
					title.Text = "Unable to execute the query! " + c.Message;
					return;
				}

				m_DataSource = query;

				if (UseOriginals.Checked)
				{
					m_SlideArray = m_dirHelper.GetPictureList(q.FileList, DirectoryHelper.FolderType.org, CurrentUser);
					m_vDirectoryPath = m_dirHelper.OriginalsVirtualRoot;
				}
				else
				{
					m_SlideArray = m_dirHelper.GetPictureList(q.FileList, DirectoryHelper.FolderType.ss, CurrentUser);
					m_vDirectoryPath = m_dirHelper.AppPath;
				}

				if (m_SlideArray.Count == 0)
				{
					title.Text = "There are no pictures that match this search criteria!";
					return;
				}

				m_StripSource = "qu=" + m_dirHelper.UrlEncodeFilePath(query);
			}
			else*/
			{
				m_DataSource = request;

				if (UseOriginals.Checked)
				{
					m_SlideArray = m_dirHelper.GetPictureList(m_DataSource, DirectoryHelper.FolderType.org, CurrentUser);
					m_vDirectoryPath = m_dirHelper.OriginalsVirtualRoot;
				}
				else
				{
					m_SlideArray = m_dirHelper.GetPictureList(m_DataSource, DirectoryHelper.FolderType.ss, CurrentUser);
					m_vDirectoryPath = m_dirHelper.AppPath;
				}

				if (m_SlideArray.Count == 0)
				{
					return;
				}

				m_StripSource = "path=" + m_dirHelper.UrlEncodeFilePath(request);
			}

			if (!IsPostBack)
			{
				DisplaySlide(0);
			}

			// Disable slide show button for non IE browsers
			HttpBrowserCapabilities bc = Request.Browser;
			if (bc.Browser == "IE" && bc.MajorVersion > 4 && bc.Platform != "WinCE")
			{
				Slide_Show.Visible = true;
			}
			else
			{
				Slide_Show.Visible = false;
				Slide_Show.CommandArgument = "No";

				if (bc.Platform == "WinCE")
				{
					title.Visible = false;
					UseOriginals.Visible = false;
					strippane.Visible = false;
					HomeLink.Visible = true;
				}
			}
		}

		protected void DisplaySlide(int i_slide)
		{
			string slidesuffix = "";
			if (i_slide >= m_SlideArray.Count)
			{
				slidesuffix = " Last Slide!";
				// If we are in slide show mode - turn it off
				if (Slide_Show.CommandArgument == "Yes")
				{
					ToggleSlideShow();
				}

				i_slide = m_SlideArray.Count - 1; // set to the last slide
			}

			if (i_slide < 0)
			{
				slidesuffix = " First Slide!";
				i_slide = 0; // set to the first slide;
			}

			FileInfo f = (FileInfo) m_SlideArray[i_slide];

			String slidepath = m_dirHelper.ReverseMapPath(f.FullName, m_vDirectoryPath);
			slide = slidepath;
			Previous.CommandArgument = Next.CommandArgument = i_slide.ToString();
			i_slide++;

			// Add the slide name. 
			Picturename.Text = f.Name;

			//Try to add the date using the EXIFF date from the image
			try
			{
				string original;

				if (UseOriginals.Checked)
					original = f.FullName;
				else
				{
					original = m_dirHelper.SL2OG(slidepath);
					original = Server.MapPath(original);
				}

				PictureProperties pp = new PictureProperties(original);
				ImageProperty propDate = pp.GetProperty(ImageProperty.ImagePropsIds.DATE_ORIGINAL);
				Picturename.Text += " taken on " + propDate.ToDateTime().ToString("ddd, MMM dd, yyyy h:mm tt");
				pp.Dispose();
			}
			catch
			{
			}


			string t = f.Directory.Name + " - " + i_slide + " of " + m_SlideArray.Count + slidesuffix;
			title.Text = t.Replace("/", " > ");
		}

		public void GoNext(object sender, CommandEventArgs e)
		{
			String sc = e.CommandArgument.ToString();
			int curr = Convert.ToInt16(sc);
			DisplaySlide(++curr);
		}

		public void GoPrevious(object sender, CommandEventArgs e)
		{
			String sc = e.CommandArgument.ToString();
			int curr = Convert.ToInt16(sc);
			DisplaySlide(--curr);
		}

		public void GoBegin(object sender, CommandEventArgs e)
		{
			DisplaySlide(0);
		}

		public void GoEnd(object sender, CommandEventArgs e)
		{
			DisplaySlide(m_SlideArray.Count - 1);
		}

		public void ToggleSlideShow(object sender, CommandEventArgs e)
		{
			ToggleSlideShow();
		}

		protected void ToggleSlideShow()
		{
			if (Slide_Show.CommandArgument == "No")
			{
				Slide_Show.CommandArgument = "Yes";
				Slide_Show.ImageUrl = "images/StopSlideShow.gif";
				Slide_Show.ToolTip = "Click here to stop the slide show";
			}
			else
			{
				Slide_Show.CommandArgument = "No";
				Slide_Show.ImageUrl = "images/SlideShow.gif";
				Slide_Show.ToolTip = "Click here to start a slide show";
			}

			String sc = Previous.CommandArgument.ToString();
			int curr = Convert.ToInt16(sc);
			DisplaySlide(curr);
		}

		protected void Page_Init(object sender, EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP+ Windows Form Designer.
			//
			InitializeComponent();
		}

		/// <summary>
		///    Required method for Designer support - do not modify
		///    the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
		}

		/// <summary>
		/// Save the state of the CheckBox in a cookie
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		protected void UseOriginals_CheckedChanged(object sender, EventArgs e)
		{
			if (UseOriginals.Checked)
			{
				HttpCookie c = new HttpCookie(ORG_COOKIE_NAME, "On");
				c.Expires = new DateTime(2020, 1, 1);
				Response.Cookies.Add(c);
			}
			else
			{
				HttpCookie c = new HttpCookie(ORG_COOKIE_NAME, "Off");
				Response.Cookies.Set(c);
			}
		}
	}
}